# -*- coding: utf-8 -*-
require 'gtk2'
require './Modele/Client.rb'
require './Modele/Parking.rb'
require './Modele/Vehicule.rb'
require './Controler/NewClient'

class LoadParking< Gtk::Builder
  def initialize(xml,titre="mabase")
      super()
      self << xml
      self['window1'].signal_connect("destroy") { onDestroy }
      self['window1'].show_all
      self.connect_signals{ |handler| method(handler) }
      @titre=titre  
      @db = SQLite3::Database.new("#{titre}.db")
      
      #CHARGEMENT DU PARKING
      initParking
      
      refresh
      
  end
  
  def refresh
    #CHARGEMENT DES CLIENTS
          clients=@db.execute2("select idclient,nom from Clients")
          
          liststore = Gtk::ListStore.new(String)
          @iter= liststore.append
          @iter.set_value(0,"Nouveau client")
    
          
          renderer = Gtk::CellRendererText.new()
          self['client'].clear
          self['client'].pack_start( renderer, true )
          self['client'].set_attributes( renderer, { "text" => 0} )
          self['client'].set_model(liststore)
          
          for i in (1...clients.length)
              self['client'].append_text("IdClient : #{clients[i][0]} , Nom : #{clients[i][1]}")
          end
          
          self['client'].active=0
          
          #CHARGEMENT DES VEHICULES
          voitures=@db.execute2("select idticket from Places")
          liststore2 = Gtk::ListStore.new(String)
          iter2= liststore2.append
          #iter2.set_value(0,"Parking vide")
          
          self['combobox1'].clear
          self['combobox1'].pack_start( renderer, true )
          self['combobox1'].set_attributes( renderer, { "text" => 0} )
          self['combobox1'].set_model(liststore2)
              
          for i in (1...voitures.length)
            self['combobox1'].append_text("#{voitures[i][0]}")
          end
          if voitures.length>1 then
            self['combobox1'].remove_text(0)       
          end
          
          self['combobox1'].active=0
  end
  
  def initParking 
    park=@db.execute2("select * from Parkings where titre='#{@titre}'")
    @Parking=Parking.new(park[1][0],park[1][1],park[1][2],true)
  end
  
  def onGarer
    dialog = Gtk::Dialog.new("Information", self, Gtk::Dialog::DESTROY_WITH_PARENT,[ Gtk::Stock::OK, Gtk::Dialog::RESPONSE_NONE ])
    dialog.signal_connect('response') { dialog.destroy }
    if self['immat'].text=='' then
            dialog.vbox.add(Gtk::Label.new("L'immatriculation doit être saisie"))
            dialog.show_all
    else      
      idCl=self['client'].active_text.split
      if(idCl[2]==nil)then#on créé un nouveau client
        NewClient.new("View/newclient.glade",@Parking,@titre,self['immat'].text)
        self['window1'].destroy
      else
        client=@db.execute2("select prenom from Clients where idclient='#{idCl[2]}'")
        client=Client.new(idCl[6],client[1][0],@Parking,idCl[2])
        str=client.se_garer(Vehicule.new(self['immat'].text),@Parking.acces[1].borne)

        self['textview1'].insert_at_cursor("\n#{str}")
        #Si le parking est complet on affiche un message
        if (/[0-9]/.match(str) == nil) then
          dialog.vbox.add(Gtk::Label.new(str))
          dialog.show_all
        else
          refresh
        end
      end
    end   
  end
  
  def onRecuperer
    client=@db.execute2("select * from Clients")
    client=Client.new(client[1][1],client[1][2],@Parking,client[1][0])
    self['textview1'].buffer.insert_at_cursor("\n#{client.reprendre_vehicule(self['combobox1'].active_text,@Parking.acces[1].borne)}")
    refresh
  end
    
  def onDestroy
      self['window1'].destroy
   end
end